package class1.s2022350143027.controller;

import class1.s2022350143027.model.GoodsModel;
import class1.s2022350143027.service.YzyGoodsService;
import class1.s2022350143027.service.Impl.YzyGoodsServiceImpl;
import class1.s2022350143027.utils.YzyValidateUtil;

import java.sql.SQLException;
import java.util.Scanner;


/**
 * 库存管理显示控制
 * <p>
 * (库存管理业务逻辑接口，库存管理页面方法)
 * @author yzy
 * @date 2024-05-02
 */
public class GoodsController {

    //声明库存业务逻辑接口
    protected YzyGoodsService goodsService = new YzyGoodsServiceImpl();

    /**
     * 库存管理页面实现
     * <p>
     * "====       0.返回首页    ===="
     * <p>
     * "====       1.商品查询    ===="
     * <p>
     * "====       2.商品录入    ===="
     * <p>
     * "====       3.商品删除    ===="
     * <p>
     * "====       4.库存增删    ===="
     * <p>
     * "====       5.库存总览    ===="
     * @author yzy
     * @date 2024-05-02
     *
     */
    //库存管理页面实现
    public void g_page() throws SQLException {
        //输入准备
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n========== 库存管理 ===========");
            System.out.println("====       0.返回首页        ====");
            System.out.println("====       1.商品查询        ====");
            System.out.println("====       2.商品录入        ====");
            System.out.println("====       3.商品删除        ====");
            System.out.println("====       4.库存增删        ====");
            System.out.println("====       5.库存总览        ====");
            System.out.println("====       6.数据保存到txt   ====");
            System.out.println("====       7.数据保存到sql   ====");
            System.out.println("====       8.从sql录入数据   ====");
            System.out.print("请输入功能对应选项:");
            String choice = scanner.next();

            switch (choice) {
                case "0"://返回首页
                    return ;

                case "1"://商品查询
                    System.out.print("请输入您需要查找的商品的名称:");
                    String lookGood_name = scanner.next();
                    goodsService.find_goods(lookGood_name);
                    break;

                case "2"://商品录入
                    //建立单个商品对象模型，储存其商品信息
                    GoodsModel goodModel = new GoodsModel();

                    System.out.print("输入商品名称:");
                    String good_name = scanner.next();

                    System.out.print("输入单价:");
                    String good_price = scanner.next();

                    System.out.print("输入库存数量:");
                    String good_number = scanner.next();

                    //满足商品信息要求就写入对应信息
                    if(!good_name.isEmpty() && YzyValidateUtil.checkGood_name(good_name) &&
                            YzyValidateUtil.checkPN(good_price) && YzyValidateUtil.checkPN(good_price)){
                        goodModel.setGoodId(goodsService.getGoodsData().getLength());
                        goodModel.setGoodName(good_name);
                        goodModel.setGoodPrice(Integer.parseInt(good_price));
                        goodModel.setGoodNumber(Integer.parseInt(good_number));

                        if (goodsService.save(goodModel)) {
                            System.out.println("录入商品信息成功！");
                            goodsService.show();
                        }

                    }
                    //否则重新操作
                    else {
                        System.out.println("录入商品信息错误，请重试！");
                        break;//返回
                    }

                    break;

                case "3"://商品删除
                    System.out.print("请输入您需要删除的商品的编号:");
                    String lookGood_id = scanner.next();
                    if(goodsService.delete(lookGood_id)) {
                        System.out.println("删除成功！");
                    }else {
                        System.out.println("没有此商品！");
                    }
                    break;

                case "4"://库存管理
                    System.out.print("请输入您需要修改的商品的编号:");
                    String lookGood_id1 = scanner.next();
                    System.out.print("请输入您对该商品库存数量操作:");
                    String number_choice = scanner.next();
                    if(!lookGood_id1.isEmpty() && !number_choice.isEmpty() && YzyValidateUtil.checkPN(number_choice)){
                        goodsService.update(lookGood_id1,Integer.parseInt(number_choice));
                    }
                    //否则重新操作
                    else {
                        System.out.println("输入格式错误，请重试！");
                        break;//返回
                    }

                    break;

                case "5"://库存总览
                    goodsService.show();
                    break;

                case "6"://保存数据到txt
                    boolean flag=goodsService.SaveDataToTxt();
                    if(flag==true){
                        System.out.println("数据保存成功,请查看YzyGoodData.txt");
                    }
                    else{
                        System.out.println("保存失败,请查看错误日志");
                    }
                    break;

                case "7"://保存数据到yzyGoods 的sql库
                    goodsService.SaveDataToSql();
                    break;

                case "8"://从sql读出数据到临时表
                    goodsService.LoadDataFromSql();
                    break;

                default://重新输入
                    System.out.println("输入的数字不正确...请重试。");
                    break;
            }
        }
    }

}
